package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsBrandService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsBrandMapper brandMapper;
    @Override
    public List<Integer> findBrandId(Integer id) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand ::getAdminId,id);
        wrapper.eq(DtsBrand ::getDeleted,false);
        wrapper.select(DtsBrand ::getId);

        List<DtsBrand> dtsBrands = brandMapper.selectList(wrapper);
        List<Integer> collect = dtsBrands.stream().map(e -> {
            return e.getId();
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> findBrandList(List<Integer> bandIds) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        if (bandIds != null && bandIds.size() > 0) {
            wrapper.in(DtsBrand::getId,bandIds);
        }
        wrapper.eq(DtsBrand ::getDeleted,false);

        List<DtsBrand> dtsBrands = brandMapper.selectList(wrapper);

        List<Map<String, Object>> collect = dtsBrands.stream().map(e -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", e.getId());
            map.put("label", e.getName());
            return map;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public DtsBrand selectById(Integer brandId) {
        DtsBrand dtsBrand = brandMapper.selectById(brandId);
        return dtsBrand;
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        IPage<DtsBrand> ipage = new Page<>(page, limit);
        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(id)) {
            wrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name",name);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        brandMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("items", ipage.getRecords());
        data.put("total", ipage.getTotal());

        return data;
    }

    @Override
    public DtsBrand insert(DtsBrand brand) {
        brand.setDeleted(false);
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brandMapper.insert(brand);
        return brand;
    }

    @Override
    public DtsBrand update(DtsBrand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
        return brand;
    }

    @Override
    public void delete(DtsBrand brand) {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getBrandId,brand.getId());
        Integer count = goodsMapper.selectCount(wrapper);
        if (count > 0) {
            throw new RuntimeException("该品牌已被关联");
        }
        brand.setDeleted(false);
        brandMapper.update(brand,null);
    }
}